package chinese.admin.dao;

import java.util.List;
import java.util.Map;

import chinese.admin.domain.ChnArticle;
import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;
/**
 * <p>数据访问类</p>
 * <p>Table: ChnArticle</p>
 *
 */
 @DAO
public interface ChnArticleDAO {

	@SQL("SELECT a.*, b.author,b.school,b.grade FROM ChnArticle a LEFT JOIN ChnAuthor b ON a.authorId=b.id WHERE a.deleteFlag=1 #if(:t.title!=''){ AND a.title LIKE '%##(:t.title)%' } #if(:t.author!=''){ AND b.author LIKE '%##(:t.author)%' } #if(:t.status!=-1){ AND a.status=:t.status } #if(:t.type!=-1){ AND a.type=:t.type } #if(:t.school!=''){ AND b.school LIKE '%##(:t.school)%' } #if(:t.grade!=-1){ AND b.grade=:t.grade } ORDER BY a.status,a.updateAt DESC LIMIT :t.start,:t.length")
	List<ChnArticle> query(@SQLParam("t")ChnArticle obj);
	
	@SQL("SELECT COUNT(1) FROM ChnArticle a LEFT JOIN ChnAuthor b ON a.authorId=b.id WHERE a.deleteFlag=1 #if(:t.title!=''){ AND a.title LIKE '%##(:t.title)%' } #if(:t.author!=''){ AND b.author LIKE '%##(:t.author)%' } #if(:t.status!=-1){ AND a.status=:t.status } #if(:t.type!=-1){ AND a.type=:t.type } #if(:t.school!=''){ AND b.school LIKE '%##(:t.school)%' } #if(:t.grade!=-1){ AND b.grade=:t.grade }")
	int querySize(@SQLParam("t")ChnArticle obj);

	@ReturnGeneratedKeys
	@SQL("INSERT INTO ChnArticle(title,content,authorId,praise,pageView,openId,type,status,year,month,yearForWeek,week,deleteFlag,createAt,createUser,updateAt,updateUser) VALUES(:t.title,:t.content,:t.authorId,:t.praise,:t.pageView,:t.openId,:t.type,:t.status,:t.year,:t.month,:t.yearForWeek,:t.week,:t.deleteFlag,:t.createAt,:t.createUser,:t.updateAt,:t.updateUser)")
	int create(@SQLParam("t")ChnArticle obj);
	
	@SQL("UPDATE ChnArticle SET title=:t.title,content=:t.content,authorId=:t.authorId,praise=:t.praise,pageView=:t.pageView,type=:t.type WHERE id=:t.id")
	void update(@SQLParam("t")ChnArticle obj);
	
	@SQL("UPDATE ChnArticle SET deleteFlag=0 WHERE id=:1")
	void delete(int id);

	@SQL("SELECT id,name FROM ChnArticleType WHERE deleteFlag=1")
	List<Map> articleTypeAsEnum();

	@SQL("SELECT * FROM	ChnArticle WHERE id=:1")
	ChnArticle queryById(int id);

	@SQL("SELECT a.*, b.author,b.school,b.grade FROM ChnArticle a LEFT JOIN ChnAuthor b ON a.authorId=b.id WHERE a.id=:1")
	ChnArticle articleById(int id);

	@SQL("UPDATE ChnArticle SET status=:2 WHERE id=:1")
	void changeStatus(int id, int status);

	@SQL("SELECT * FROM	ChnArticle WHERE deleteFlag=1 and authorId=:t.authorId and status=:t.status order by updateAt desc limit :t.start,:t.length")
	List<ChnArticle> queryArticles(@SQLParam("t")ChnArticle article);

	@SQL("SELECT count(*) FROM ChnArticle WHERE deleteFlag=1 and authorId=:t.authorId and status=:t.status")
	int queryArticlesSize(@SQLParam("t")ChnArticle article);

}